home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5939 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: bubba.NMSU.Edu!usenet
  2. From: ghenniga@ampere.NMSU.Edu (Gary Hennigan)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Ability to locate spaces?
  5. Date: 21 Feb 1996 10:25:51 -0700
  6. Organization: New Mexico State University - Electromagnetics Group
  7. Sender: ghenniga@ampere.NMSU.Edu
  8. Message-ID: <rhtenro1tgg.fsf@ampere.NMSU.Edu>
  9. References: <Pine.SOL.3.91.960215222301.15979A-100000@teer1.acpub.duke.edu>
  10.     <harmon.824449354@pegasus.montclair.edu>
  11. Reply-To: ghenniga@NMSU.Edu
  12. NNTP-Posting-Host: ampere.nmsu.edu
  13. In-reply-to: harmon@pegasus.montclair.edu's message of 16 Feb 1996 01:06:54
  14.     -0500
  15. X-Newsreader: Gnus v5.1
  16.  
  17. In an article harmon@pegasus.montclair.edu (Derek Harmon) wrote:
  18. |>string = fgets (buf, 199, input);
  19. | ^ BANG!  Unless you snuck a string = (char *)malloc( ... somewhere before
  20. | this point, string remains an uninitialized pointer.  It's worth noting that
  21. | fgets() returns the same string that it reads into buf.  There is no reason
  22. | to keep its return value here, and you could discard it with,
  23. |: fgets(buf, 200, input);  /* You don't use buf as a string, so you are */
  24. |:                          /* technically allowed all 200 bytes if you */
  25. |:                          /* don't intend to (otherwise buf[199] = '\0' */
  26. |:                          /* is a good idea). */
  27.  
  28. Actually, you'll want to check your man page/manual on fgets()
  29. behavior. At least on all the Unix implementations I've seen,
  30. fgets(buf, 200, input) would read 199 characters (or to a newline
  31. character) and then terminate the string with a NULL character
  32. automatically. Thus, there is no need to manually terminate anything
  33. read by fgets() or use one less than the size of the character array
  34. into which the string would be read in the fgets() argument.
  35.  
  36. Also, you don't need to allocate "string" above. fgets() returns a
  37. pointer to where the string resides. In this case string would've been
  38. assigned as:
  39.         string = &buf[0];
  40.  
  41. by fgets(), assuming, of course, the read was successful.
  42.  
  43. In other words...READ the man page/manual!!
  44.  
  45. Gary
  46. (ghenniga@NMSU.Edu)
  47.